package NBU::Image;
require 5.002;

use strict;
use Carp;

my %imageList;

BEGIN {
  use Exporter   ();
  use AutoLoader qw(AUTOLOAD);
  use vars       qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $AUTOLOAD);
  $VERSION =     '0.01';
  @ISA =         qw();
  @EXPORT =      qw();
  @EXPORT_OK =   qw();
  %EXPORT_TAGS = qw();
}

sub new {
  my $Class = shift;
  my $image = { };

  bless $image, $Class;

  if (@_) {
    $image->backupID(shift);
    if (exists($imageList{$image->backupID})) {
    }
  }
  return $image;
}

sub byBackupID {
  my $Class = shift;
  my $backupID = shift;

  if (exists($imageList{$backupID})) {
    return $imageList{$backupID};
  }

  return undef;
}

sub backupID {
  my $self = shift;

  if (@_) {
    $self->{BACKUPID} = shift;
    # The backup ID of an image in part encodes the time the image was
    # created
    $self->{CTIME} = ($self->backupID, -10);
  }
  return $self->{BACKUPID};
}

sub ctime {
  my $self = shift;

  return $self->{CTIME};
}

sub client {
  my $self = shift;

  if (@_) {
    $self->{CLIENT} = shift;
  }
  return $self->{CLIENT};
}


sub class {
  my $self = shift;

  if (@_) {
    $self->{CLASS} = shift;
  }
  return $self->{CLASS};
}

sub loadFileList {
  my $self = shift;
  my @fl;
 
  my ($s, $m, $h, $dd, $mon, $year, $wday, $yday, $isdst) = localtime($self->ctime);
  my $mm = $mon + 1;
  my $yy = sprintf("%02d", ($year + 1900) % 100);
  open(PIPE, $NBU::prefix."netbackup/bin/admincmd/bpflist -t ANY".
    " -option GET_ALL_FILES".
    " -client ".$self->client.
    " -backupid ".$self->backupID.
    " -d ${mm}/${dd}/${yy}"." |");
  while (<PIPE>) {
  }
  close(PIPE);
}

sub fileList {
  my $self = shift;

  if (!$self->{FLIST}) {
    $self->loadFileList;
  }

  if (my $flR = $self->{FLIST}) {
    return @$flR;
  }
  return undef;
}

1;

__END__
